import API from './index';

// 获取热门路线
export const getHotRoutes = (params = {}) => {
  return API.get('/routes/hot', { params }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};

// 获取路线详情
export const getRouteById = (id) => {
  return API.get(`/routes/${id}`).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return {};
  });
};

// 分页获取路线列表
export const pageRoutes = (page = 1, size = 10, params = {}) => {
  return API.get('/routes', {
    params: {
      current: page, // 注意这里使用 current 而不是 page，与后端接口对应
      size,
      ...params
    }
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return { records: [], total: 0 };
  });
};

// 获取路线评论
export const getRouteReviews = (routeId, params = {}) => {
  return API.get(`/routes/${routeId}/reviews`, { params }).then(res => {
    if (res.data && res.data.code === 200) {
      // 直接返回 data 对象，包含 records 和 total
      return res.data.data || { records: [], total: 0 };
    }
    return { records: [], total: 0 };
  });
};

// 提交路线评论
export const submitRouteReview = (routeId, data) => {
  return API.post(`/routes/${routeId}/reviews`, data);
};

// 获取路线筛选选项
export const getRouteFilterOptions = () => {
  return API.get('/routes/filter-options').then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return {
      themes: [],
      difficulties: [],
      durations: []
    };
  });
};

// 获取相关路线
export const getRelatedRoutes = (destinationId, limit = 4) => {
  return API.get(`/routes/related/${destinationId}`, { 
    params: { limit } 
  }).then(res => {
    if (res.data && res.data.code === 200) {
      return res.data.data;
    }
    return [];
  });
};